package io.gitee.yxsnake.embark.web.core.interceptor;

import com.github.lianjiatech.retrofit.spring.boot.interceptor.GlobalInterceptor;
import io.gitee.yxsnake.embark.web.core.constant.Commons;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;
import org.slf4j.MDC;

import java.io.IOException;

/**
 * @author snake
 * @description  OKHttp 添加traceId 拦截器
 * @since 2024/10/17 0:02
 */
public class RetrofitTraceIdInterceptor implements GlobalInterceptor {
    @Override
    public Response intercept(Chain chain) throws IOException {
        String traceId = MDC.get(Commons.TRACE_ID);
        Request request = null;
        if (traceId != null) {
            //添加请求体
            request = chain.request().newBuilder().addHeader(Commons.TRACE_ID, traceId).build();
        }
        Response originResponse = chain.proceed(request);

        return originResponse;
    }
}
